﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;
using System.Web.Core;
using System.Collections.Specialized;

namespace Yuanlin.Business
{
    public class SendMessage : YuanlinUserControl
    {
        protected ValuedTextBox visitorNameValuedTextBox;
        protected ValuedTextBox addressValuedTextBox;
        protected ValuedTextBox contactValuedTextBox;
        protected ValuedTextBox greenNumberValuedTextBox;
        protected ValuedTextBox bodyValuedTextBox;
        protected HtmlControl divHidden;
        protected IButton SaveButton;
        private YuanlinUser yuanlinUser = HttpContext.Current.User as YuanlinUser;
        private string greenNum = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.GreenNum);

        protected override void OnLoad(EventArgs e)
        {
            if (Page.IsPostBack)
            {
                if (!ValidateAuthCode(HttpContext.Current.Request.Form))
                {
                    Globals.ShowMessage(Page, "AuthCodeError", "验证码输入不正确！");
                    return;
                }
            }
            else
            {
                if (!yuanlinUser.IsAnonymous)
                {
                    divHidden.Visible = false;
                }
            }
            SaveButton.Click += new EventHandler(SaveButton_Click);
        }

        public void SaveButton_Click(object sender, EventArgs e)
        {
            CompanyMessage companyMessage = new CompanyMessage();

            if (yuanlinUser.IsAnonymous)
            {
                companyMessage.VisitorUserName.Value = visitorNameValuedTextBox.Text;
                companyMessage.Address.Value = addressValuedTextBox.Text;
                companyMessage.Contact.Value = contactValuedTextBox.Text;
                companyMessage.IpAddress.Value = IpClass.GetIp();
            }
            companyMessage.GreenNumber.Value = greenNumberValuedTextBox.Text;
            companyMessage.Body.Value = this.bodyValuedTextBox.Value;
            companyMessage.UserId.Value = UserIdFromGreenNum;
            companyMessage.CreateDate.Value = DateTime.Now;

            BusinessManager.CreateCompanyMessage(companyMessage);
            HttpContext.Current.Response.Redirect(SiteUrls.Instance().GetUserSendMessageSuccessUrl(greenNum));
        }

        private bool ValidateAuthCode(NameValueCollection formData)
        {
            if (HttpContext.Current.Request.Cookies["AuthCode"] != null &&
                HttpContext.Current.Request.Cookies["AuthCode"].Value != null &&
                HttpContext.Current.Request.Cookies["AuthCode"].Value == formData["number"])
            {
                return true;
            }
            return false;
        }
    }
}
